#include <iostream>
#include <algorithm>
using namespace std;

int const maxn = 4000;
int const oo = 1<<29;
int f[maxn][2];
int da[maxn];
int n, b;

int main()
{
	ios::sync_with_stdio(false);
	cin >> n >> b;
	for (int i = 0; i < n; i++) cin >> da[i];

	f[0][0] = 0; f[0][1] = -oo;
	f[1][1] = 0; f[1][0] = -oo;
	int ans = 0;
	for (int i = 1; i < n; i++) {
		for (int j = min(i+1, b); j > 0; j--) {
			if (i >= j) f[j][0] = max(f[j][1], f[j][0]);
			f[j][1] = max(f[j-1][0], da[i] + f[j-1][1]);
		}
		f[0][0] = 0; f[0][1] = -oo;
		ans = max(ans, f[b][1]);
	}

	for (int j = 0 ; j <= b; j++) f[j][0] = f[j][1] = 0;
	f[0][0] = 0; f[0][1] = -oo;
	f[1][1] = da[0]; f[1][0] = -oo;
	for (int i = 1; i < n; i++) {
		for (int j = min(i+1, b); j > 0; j--) {
			if (i >= j) f[j][0] = max(f[j][1], f[j][0]);
			f[j][1] = max(f[j-1][0], da[i] + f[j-1][1]);
		}
		f[0][0] = 0; f[0][1] = -oo;
	}

	ans = max(ans, f[b][1]);
	cout << ans << "\n";
}
